package anonymouschat.messages.clientrequests;

import anonymouschat.client.Identity;
import anonymouschat.client.PublicIdentity;
import anonymouschat.security.SecurityUtils;
import anonymouschat.security.integrity.DigSig;

/**
 * The initial message sent to a server, and the publication server broadcast
 * this to all the servers that are alive.
 * 
 * @author bh349
 */
public class UserLoginRequest extends ClientRequest {
	private static final long serialVersionUID = -6394737131192996359L;

	// the current system time used as a timestamp
	private long announceTime = 0;

	// the server this client is going to published at
	private String publicationServer = null;

	// the public keys of this user
	private PublicIdentity pubIdent = null;

	// the signed combination of the destination server and the time stamp
	private DigSig signedTS = null;

	/**
	 * 
	 * @param fullIdent
	 *            the identity of the user logging in
	 */
	public UserLoginRequest(Identity fullIdent, String wherePublish) {
		super(fullIdent.getUserName());
		pubIdent = new PublicIdentity(fullIdent);
		announceTime = System.currentTimeMillis();
		publicationServer = wherePublish;
		String signedComponent = getSignedComponent();
		signedTS = SecurityUtils.sign(signedComponent,
				fullIdent.getSigningKey());
	}

	public UserLoginRequest(String userID) {
		super(userID);
	}

	public void setAnnounceTime(long announceTime) {
		this.announceTime = announceTime;
	}

	public long getAnnounceTime() {
		return announceTime;
	}

	public void setPublicationServer(String publicationServer) {
		this.publicationServer = publicationServer;
	}

	public String getPublicationServer() {
		return publicationServer;
	}

	public PublicIdentity getPubIdent() {
		return pubIdent;
	}

	public void setPubIdent(PublicIdentity pubIdent) {
		this.pubIdent = pubIdent;
	}

	public DigSig getSig() {
		return this.signedTS;
	}

	/**
	 * Get the part of the message which is to be signed
	 * 
	 * @return
	 */
	public String getSignedComponent() {
		return publicationServer + announceTime;
	}

	@Override
	public String getAllContents() {
		return "UserLoginRequest:\n\tannounceTime: "
				+ super.fixNull(this.announceTime) + "\n\tpubSrv: "
				+ super.fixNull(this.publicationServer) + "\n\tUser: "
				+ super.fixNull(this.pubIdent.getUserName());
	}

}
